<?php

namespace app\api\controller;

use app\common\controller\Common;
use think\Request;
use think\Db;

class Group extends Base
{


    public function groupList(){
        $param = $this->param;
        $page = !empty($param['page']) ? $param['page']-1 : 0;
        $row = !empty($param['row']) ? $param['row'] : 10;
        $limit = $page*$row;
        $count = Db::name('admin_group')->count('id');
        $list = Db::name('admin_group')->where($where)->limit($limit,$row)->select();
		if($list){
			foreach ($list as $key => &$value) {
				if($value['status'] == 1){
					$value['status'] = '正常';
				}else{
					$value['status'] = '冻结';
                }
                $value['create_time'] = date('Y-m-d H:m:s', $value['create_time']);
                $value['update_time'] = date('Y-m-d H:m:s', $value['update_time']);
			}
        }
        $return['total'] = $count;
        $return['totalPage'] = ceil($count/$row);
        $return['list'] = $list;
		return resultArray(['data' => $return]);
    }
    

    public function groupOne(){
        $param = $this->param;
        if($param['id']){
            $where['id'] = intval($param['id']);
            $res = Db::name('admin_group')->where($where)->find();
            if($res){
                return resultArray(['data' => $res]);
            }
            return resultArray(['error' => '分组不存在']);
        }else{
            return resultArray(['error' => '参数缺失']);
        }
    }



    public function groupAdd(){
        $param = $this->param;
        $param['title'] = trim($param['title']);
        if($param['rule']){
            $rule = implode(',',$param['rule']);
        }
        if($param['title']){
            $check = Db::name('admin_group')->where(['title'=>$param['title']])->find();
            if(!$check){
                $data['title'] = $param['title'];
                $data['pid'] = $param['pid'];
                $data['rules'] = $rule;
                $data['remark'] = $param['remark'];
                $data['status'] = 1;
                $data['create_time'] = $data['update_time'] = time();
                $res = Db::name('admin_group')->insert($data);
                if($res){
                    return  resultArray(['data' => '添加成功']);
                }
                return  resultArray(['error' => '添加失败']);
            }
            return  resultArray(['error' => '分组名已存在']);
        }
        return  resultArray(['error' => '分组名不能为空']);
    }



    public function groupEdit(){
        $param = $this->param;
        $param['title'] = trim($param['title']);
        if($param['rule']){
            $rule = implode(',',$param['rule']);
        }
        if($param['title']){
            $where['id'] = array('neq',intval($param['id']));
            $where['title'] = $param['title'];
            $check = Db::name('admin_group')->where($where)->find();
            if(!$check){
                $data['title'] = $param['title'];
                $data['pid'] = $param['pid'];
                $data['rules'] = $rule;
                $data['remark'] = $param['remark'];
                $data['status'] = 1;
                $data['update_time'] = time();
                $res = Db::name('admin_group')->where(array('id'=>intval($param['id'])))->update($data);
                return  resultArray(['data' => '修改成功']);
            }
            return  resultArray(['error' => '分组名已存在']);
        }
        return  resultArray(['error' => '分组名不能为空']);
    }


    
    public function groupDel(){
        $param = $this->param;
        $param['id'] = intval($param['id']);
        $where['user_group'] = $param['id'];
        $list = Db::name('admin_user')->where($where)->select();
        if($list){
            Db::startTrans();
            foreach ($list as $key => $value) {
                $map['id'] = $value['id'];
                $data['user_group'] = 1;
                try{
                    $userChange = Db::name('admin_user')->where($map)->update($data);
                    if(!$userChange){
                        Db::rollback();
                        return  resultArray(['error' => '删除失败']);
                    }
                }catch(\Exception $e){
                    Db::rollback();
                    return  resultArray(['error' => '删除失败']);
                }       
            }
            $res = Db::name('admin_group')->where(array('id'=>$param['id']))->delete();
            if($res){
                Db::commit();
                return  resultArray(['data' => '删除成功']);
            }else{
                Db::rollback();
                return  resultArray(['error' => '删除失败']);
            }    
        }
        $res = Db::name('admin_group')->where(array('id'=>$param['id']))->delete();
        if($res){
            return  resultArray(['data' => '删除成功']);
        }
        return  resultArray(['error' => '删除失败']);

    }
}
?>





  